package yxy.game.pm2.bean.db.newDao;

import yxy.apple.db.DbManager;
import yxy.game.pm2.bean.db.po.OrderInfoPo;
import yxy.game.pm2.bean.db.po.StairwayRecordPo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liangjianbin
 * @date 2022/3/1 8:40
 **/
public class StairwayRecordDAO {
    private static final String
            REPLACE_ONE = "replace into `stairwayRecord` (`pCode`,`date`,`recordCode`,`isAttacker`,`rank`,`name`,`head`," +
            "`headframe`,`level`,`power`,`battleResult`,`updateTime`) " +
            "values (?,?,?,?,?,?,?,?,?,?,?,?)",
            SELECT_ALL = "select * from `stairwayRecord` where `pCode` = ?",
            DELETE = "delete from `stairwayRecord` where `pCode` = ? and `updateTime` != ?";

    public static List<StairwayRecordPo> getList(String playerCode) {
        return DbManager.instance().executeQueryObjectList(SELECT_ALL, new Object[]{playerCode}, StairwayRecordPo.builder);
    }

    public static boolean replace(List<StairwayRecordPo> stairwayRecordPoList) {
        List<Object[]> list = new ArrayList<>();
        for (StairwayRecordPo stairwayRecordPo : stairwayRecordPoList) {
            Object[] objects = new Object[]{stairwayRecordPo.getPlayerCode(), stairwayRecordPo.getDate(), stairwayRecordPo.getRecordCode(),
                    stairwayRecordPo.isAttacker(), stairwayRecordPo.getRank(), stairwayRecordPo.getName(), stairwayRecordPo.getHead(),
                    stairwayRecordPo.getHeadframe(), stairwayRecordPo.getLevel(), stairwayRecordPo.getPower(), stairwayRecordPo.getBattleResult(),
                    stairwayRecordPo.getUpdateTime()};
            list.add(objects);
        }
        int[] ints = DbManager.instance().executeBatchCommand(REPLACE_ONE, list);
        return ints.length == stairwayRecordPoList.size();
    }

    public static void delete(String pCode, long updateTime) {
        int i = DbManager.instance().executeCommand(DELETE, new Object[]{pCode, updateTime});

    }

    public static StairwayRecordPo findOne(String playerCode) {
        String sql = "select * from `stairwayRecord` where `pCode` = ? ";
        List<StairwayRecordPo> list = DbManager.instance().executeQueryObjectList(sql, new Object[]{playerCode}, StairwayRecordPo.builder);
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }
}
